/* 
 * File:   ClientConfFileManager.cpp
 * Author: nahuel
 * 
 * Created on May 18, 2013, 5:51 PM
 */

#include "common_ClientConfFileManager.h"
#include <sstream>
using namespace TP;

std::string ClientConfFileManager::generalNodeName = "General";
    std::string ClientConfFileManager::pathAuNodeName = "PathAU";
    std::string ClientConfFileManager::intervaloPollingNodeName = "IntervaloPolling";
        
std::string ClientConfFileManager::conexionNodeName = "Conexion";
    std::string ClientConfFileManager::ipServidorNodeName = "IpServidor";
    std::string ClientConfFileManager::puertoServidorNodeName = "PuertoServidor";
    std::string ClientConfFileManager::usuarioNodeName = "Usuario";
    std::string ClientConfFileManager::passwordNodeName = "Password";

ClientConfFileManager::ClientConfFileManager(const std::string& filePath) {
    _confFile = new TiXmlDocument(filePath.c_str());
    _filePath = filePath;
	
    if (!_confFile->LoadFile()) {
        _CrearArchivo();
    }	
}

void ClientConfFileManager::_CrearArchivo() {
    _confFile = new TiXmlDocument();
    
    TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "", "" );
    
	TiXmlElement* generalNode = new TiXmlElement(generalNodeName.c_str());
    TiXmlElement* pathAuNode = new TiXmlElement(pathAuNodeName.c_str());
    TiXmlElement* intervaloPollingNode = new TiXmlElement(intervaloPollingNodeName.c_str());
    generalNode->LinkEndChild(pathAuNode);
    generalNode->LinkEndChild(intervaloPollingNode);    
    
    
    TiXmlElement* conexionNode = new TiXmlElement(conexionNodeName.c_str());
    TiXmlElement* ipServidorNode = new TiXmlElement(ipServidorNodeName.c_str());
    TiXmlElement* puertoServidorNode = new TiXmlElement(puertoServidorNodeName.c_str());
    TiXmlElement* usuarioNode = new TiXmlElement(usuarioNodeName.c_str());
    TiXmlElement* passwordNode = new TiXmlElement(passwordNodeName.c_str());
    conexionNode->LinkEndChild(ipServidorNode);
    conexionNode->LinkEndChild(puertoServidorNode);
    conexionNode->LinkEndChild(usuarioNode);
    conexionNode->LinkEndChild(passwordNode);
    
    _confFile->LinkEndChild(decl);
    _confFile->LinkEndChild(generalNode);
    _confFile->LinkEndChild(conexionNode);   
    
    // TODO: Deletes??
}
        
std::string ClientConfFileManager::GetPathAU() {
    TiXmlNode* generalNode = _confFile->FirstChild(generalNodeName.c_str());
    TiXmlNode* pathAuNode = generalNode->FirstChild(pathAuNodeName.c_str());
    TiXmlNode* innerNode = pathAuNode->FirstChild();
    
    if (innerNode) {        
        return std::string(innerNode->Value());
    } else {
        return "";
    }    
}

unsigned int ClientConfFileManager::GetIntervaloPolling() {
    TiXmlNode* generalNode = _confFile->FirstChild(generalNodeName.c_str());
    TiXmlNode* intervaloPollingNode = generalNode->FirstChild(intervaloPollingNodeName.c_str());
    TiXmlNode* innerNode = intervaloPollingNode->FirstChild();
    
    std::string intervaloPollingStr;
    
    if (innerNode) {
        intervaloPollingStr = std::string(innerNode->Value());
        
        std::stringstream ss(intervaloPollingStr);
        unsigned int intervaloPolling;
        ss >> intervaloPolling;

        return intervaloPolling;
    } else {
        return 10;      // DEFAULT VALUE
    }    
}

std::string ClientConfFileManager::GetIpServidor() {
    TiXmlNode* conexionNode = _confFile->FirstChild(conexionNodeName.c_str());
    TiXmlNode* ipNode = conexionNode->FirstChild(ipServidorNodeName.c_str());
    TiXmlNode* innerNode = ipNode->FirstChild();
    
    if (innerNode) {        
        return std::string(innerNode->Value());
    } else {
        return "";
    }   
}

std::string ClientConfFileManager::GetPuertoServidor() {
    TiXmlNode* conexionNode = _confFile->FirstChild(conexionNodeName.c_str());
    TiXmlNode* puertoNode = conexionNode->FirstChild(puertoServidorNodeName.c_str());
    TiXmlNode* innerNode = puertoNode->FirstChild();
    
    std::string puertoStr;
    
    if (innerNode) {
        puertoStr = std::string(innerNode->Value());
    } else {
        puertoStr = "8080";      // DEFAULT VALUE
    }    
    return puertoStr;
}

std::string ClientConfFileManager::GetUsuario() {
    TiXmlNode* conexionNode = _confFile->FirstChild(conexionNodeName.c_str());
    TiXmlNode* usuarioNode = conexionNode->FirstChild(usuarioNodeName.c_str());
    TiXmlNode* innerNode = usuarioNode->FirstChild();
    
    if (innerNode) {        
        return std::string(innerNode->Value());
    } else {
        return "";
    }   
}

std::string ClientConfFileManager::GetPassword() {
    TiXmlNode* conexionNode = _confFile->FirstChild(conexionNodeName.c_str());
    TiXmlNode* passwordNode = conexionNode->FirstChild(passwordNodeName.c_str());
    TiXmlNode* innerNode = passwordNode->FirstChild();
    
    if (innerNode) {        
        return std::string(innerNode->Value());
    } else {
        return "";
    }   
}

void ClientConfFileManager::SetPathAU(const std::string& pathAu) {
    TiXmlNode* generalNode = _confFile->FirstChild(generalNodeName.c_str());
    TiXmlNode* pathAuNode = generalNode->FirstChild(pathAuNodeName.c_str());
    pathAuNode->RemoveChild(pathAuNode->FirstChild());
    
    TiXmlText* nuevoPath = new TiXmlText(pathAu.c_str());
    pathAuNode->LinkEndChild(nuevoPath);    
}

void ClientConfFileManager::SetIntervaloPolling(unsigned int intervaloPolling) {
    TiXmlNode* generalNode = _confFile->FirstChild(generalNodeName.c_str());
    TiXmlNode* intervaloPollingNode = generalNode->FirstChild(intervaloPollingNodeName.c_str());
    intervaloPollingNode->RemoveChild(intervaloPollingNode->FirstChild());
    
    std::stringstream ss;
    ss << intervaloPolling;
    std::string intervaloPollingStr = ss.str();
    
    TiXmlText* nuevoUsuario = new TiXmlText(intervaloPollingStr.c_str());
    intervaloPollingNode->LinkEndChild(nuevoUsuario);
}

void ClientConfFileManager::SetIpServidor(const std::string& ipServidor) {
    TiXmlNode* conexionNode = _confFile->FirstChild(conexionNodeName.c_str());
    TiXmlNode* ipServidorNode = conexionNode->FirstChild(ipServidorNodeName.c_str());
    ipServidorNode->RemoveChild(ipServidorNode->FirstChild());
    
    TiXmlText* nuevaIp = new TiXmlText(ipServidor.c_str());
    ipServidorNode->LinkEndChild(nuevaIp);
}

void ClientConfFileManager::SetPuertoServidor(const std::string& puertoServidor) {
    TiXmlNode* conexionNode = _confFile->FirstChild(conexionNodeName.c_str());
    TiXmlNode* puertoNode = conexionNode->FirstChild(puertoServidorNodeName.c_str());
    puertoNode->RemoveChild(puertoNode->FirstChild());
    
    TiXmlText* nuevoUsuario = new TiXmlText(puertoServidor.c_str());
    puertoNode->LinkEndChild(nuevoUsuario);
}

void ClientConfFileManager::SetUsuario(const std::string& usuario) {
    TiXmlNode* conexionNode = _confFile->FirstChild(conexionNodeName.c_str());
    TiXmlNode* usuarioNode = conexionNode->FirstChild(usuarioNodeName.c_str());
    usuarioNode->RemoveChild(usuarioNode->FirstChild());
    
    TiXmlText* nuevoUsuario = new TiXmlText(usuario.c_str());
    usuarioNode->LinkEndChild(nuevoUsuario);
}

void ClientConfFileManager::SetPassword(const std::string& password) {
    TiXmlNode* conexionNode = _confFile->FirstChild(conexionNodeName.c_str());
    TiXmlNode* passwordNode = conexionNode->FirstChild(passwordNodeName.c_str());
    passwordNode->RemoveChild(passwordNode->FirstChild());
    
    TiXmlText* nuevaPassword = new TiXmlText(password.c_str());
    passwordNode->LinkEndChild(nuevaPassword);
}

void ClientConfFileManager::SaveChanges() {
    _confFile->SaveFile(_filePath.c_str());
}

ClientConfFileManager::~ClientConfFileManager() {    
    if (_confFile) delete _confFile;
}